package com.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bean.ReturnMsg;
import com.bean.StorerTransfer;
import com.bean.StorerTransferCancel;
import com.bean.StorerTransferDetail;
import com.bean.wms.PbTransfer;
import com.bean.wms.PbTransferDetail;
import com.dao.StorerTransferDao;
import com.dao.wmsdao.PbTransferDao;
import com.dao.wmsdao.VerifyDao;
import com.domain.beanutil.BeanConvertUtil;
import com.domain.beanutil.DataUtilInterface;
import com.service.PbTransferService;
import com.service.VerifyService;

/** 
* @ClassName: PbTransferServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author wangxm
* @date 2014-6-16 下午4:16:25 
*/
@Service("pbTransferService")
@Transactional(rollbackFor=Exception.class)
public class PbTransferServiceImpl implements PbTransferService {
	
	@Autowired 
	private PbTransferDao pbTransferDao;
	
	@Autowired
	private StorerTransferDao storerTransferDao;
	
	@Autowired
	private VerifyDao verifyDao;
	
	@Autowired
	private VerifyService verifyService;

	@Override
	public ReturnMsg getStorerTransfer(StorerTransfer storerTransfer) {
		PbTransfer pbTransfer = BeanConvertUtil.storerTransferConvertPbTransfer(storerTransfer);
		ReturnMsg returnMsg = new ReturnMsg(storerTransfer.getFstorer(), storerTransfer.getWmwhseid(), storerTransfer.getExternalno(), "", "", "");
		boolean flag = true;
		String errMsg = "";
		if(pbTransfer.getWhseid() == null ||"".equals(pbTransfer.getWhseid())){
			pbTransfer.setWhseid(DataUtilInterface.defaultwhseid);
		}
		errMsg = verifyService.verifyPbTransfer(pbTransfer);
		if(!"".equals(errMsg)){
			returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
    		returnMsg.setErrmsg(errMsg);
			return returnMsg;
		}
		PbTransfer pbTransfer2 = pbTransferDao.getPbTransferByExternalno(pbTransfer.getWhseid(), pbTransfer.getExternalno(), pbTransfer.getFshippers(), pbTransfer.getTshippers());
		try {
			if(pbTransfer2 == null){
				pbTransferDao.insertPbTransfer(pbTransfer);
				for (PbTransferDetail pbTransferDetail : pbTransfer.getItemList()) {
					pbTransferDetail.setTransferkey(pbTransfer.getTransferkey());
					
					if(verifyDao.verifyPbTransferDetail(pbTransfer.getWhseid(), pbTransferDetail.getExternalno(), pbTransferDetail.getFshippers(), pbTransferDetail.getSku(), pbTransferDetail.getExternallineno()) == 0){
						pbTransferDao.insertPbTransferDetail(pbTransferDetail);
					}else{
						pbTransferDao.updatePbTransferDetail(pbTransferDetail);
					}
				}
			}else{
				pbTransfer.setTransferkey(pbTransfer2.getTransferkey());
				pbTransferDao.updatePbTransfer(pbTransfer);
				for (PbTransferDetail pbTransferDetail : pbTransfer.getItemList()) {
					
					pbTransferDetail.setTransferkey(pbTransfer.getTransferkey());
					
					if(verifyDao.verifyPbTransferDetail(pbTransfer.getWhseid(), pbTransferDetail.getExternalno(), pbTransferDetail.getFshippers(), pbTransferDetail.getSku(), pbTransferDetail.getExternallineno()) == 0){
						pbTransferDao.insertPbTransferDetail(pbTransferDetail);
					}else{
						pbTransferDao.updatePbTransferDetail(pbTransferDetail);
					}
				}
			}
			if(flag){
				storerTransferDao.insertStorerTransfer(storerTransfer);
				for (StorerTransferDetail storerTransferDetail : storerTransfer.getItemList()) {
					storerTransferDao.insertStorerTransferDetail(storerTransferDetail);
				}
				returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
				returnMsg.setNotes(DataUtilInterface.NOTES_SUC);
			}
		} catch (Exception e) {
			errMsg = pbTransfer.getExternalno() + DataUtilInterface.ERROR_MSG_INSERT_ERROR;
    		returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
    		returnMsg.setErrmsg(errMsg+","+e.getMessage());
    		flag = false;
			e.printStackTrace();
		}
		return returnMsg;
	}

	@Override
	public ReturnMsg getStorerTransferCancel(StorerTransferCancel storerTransferCancel) {
		PbTransfer pbTransfer = pbTransferDao.getPbTransferByExternalno(storerTransferCancel.getWmwhseid(), storerTransferCancel.getExternalno());
		ReturnMsg returnMsg = new ReturnMsg(storerTransferCancel.getStorer(), storerTransferCancel.getWmwhseid(), storerTransferCancel.getExternalno(), "", "", "");
	    if(pbTransfer == null){
	    	returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
	    	returnMsg.setErrmsg(DataUtilInterface.ERROR_MSG_NO_DATA);
	    	return returnMsg;
	    }
		if(pbTransfer.getWhseid() == null ||"".equals(pbTransfer.getWhseid())){
			pbTransfer.setWhseid(DataUtilInterface.defaultwhseid);
		}
		pbTransfer.setBilltype(storerTransferCancel.getType());
		pbTransfer.setFshippers(storerTransferCancel.getStorer());
		pbTransfer.setReason(storerTransferCancel.getReason());
		pbTransfer.setStatus("20");
	    try {
			pbTransferDao.updatePbTransfer(pbTransfer);
		} catch (Exception e) {
			e.printStackTrace();
			returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
			returnMsg.setErrmsg(DataUtilInterface.CANCEL_ERROR_MSG_ERROR+","+e.getMessage());
			System.out.println(e.getMessage());
			return returnMsg;
		}
	    returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_Y);
        storerTransferDao.insertStorerTransferCancel(storerTransferCancel);
		return returnMsg;
	}
}
